﻿/*
 * Created by SharpDevelop.
 * User: lpelegrini
 * Date: 22/06/2013
 * Time: 20:38
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Data.SqlClient;
using Fazenda.Entity;
using System.Data.Common;
using System.Data;
using System.Collections.Generic;

namespace Fazenda.DAL
{
	/// <summary>
	/// Description of PersonDAL.
	/// </summary>
	public class PersonDAL<T> : IDBAccess<T> where T : Person
	{
		//private DatabaseHelper databaseHelper;
		public PersonDAL()
		{
			//databaseHelper = DatabaseHelper.Create();
		}
		
		public int SaveRegistry(T registry)
		{
			
			try
			{
				List<DbParameter> param = new List<DbParameter>();
				string query = "insert into Pessoa(nome,tipo,natureza,documento) values (nome,tipo,natureza,documento); select @@IDENTITY;";
				param.Add(ConnectionDB.createParameter("@nome", DbType.String, registry.Name ));
				param.Add(ConnectionDB.createParameter("@tipo", DbType.Int32, registry.Category));
				param.Add(ConnectionDB.createParameter("@natureza", DbType.Int32, registry.PersonType));
				param.Add(ConnectionDB.createParameter("@documento", DbType.String, registry.Document));
				ConnectionDB.executeCommand(query, CommandType.StoredProcedure,param,TypeCommand.ExecuteScalar);
				registry.ID = Convert.ToInt32 (ConnectionDB.executeCommand(query, CommandType.StoredProcedure,param,TypeCommand.ExecuteScalar));
			}
			catch (SqlException ex)
			{
				throw new Exception ("Servidor SQL Erro:" + ex.Number);
			}
			catch (Exception ex)
			{
				throw new Exception (ex.Message);
			}
			return registry.ID;
		}
		
		public bool UpdateRegistry(T registry)
		{
			throw new NotImplementedException();
		}
		
		public System.Collections.Generic.List<T> GetRegistry(T registry)
		{
			throw new NotImplementedException();
		}
		
		public System.Collections.Generic.List<T> GetAll()
		{
			throw new NotImplementedException();
		}
		
		public T GetRegistryById(int id)
		{
			Person person = new Person();
			DbDataReader reader = null;
			List<DbParameter> param = new List<DbParameter>();
			
			try
			{
				string query = "SELECT * FROM Person WHERE id = @id";
				param.Add(ConnectionDB.createParameter("@id", DbType.Int32, id));
				
				//databaseHelper.OpenConnection();
				//reader = databaseHelper.executeCommand(query,new MySqlParameter("id", id));
				
				reader = (DbDataReader)ConnectionDB.executeCommand(query, CommandType.Text, param, TypeCommand.ExecuteReader);
				
				while (reader.Read())
				{
					person.ID = Convert.ToInt32(reader["id"]);
					person.Name = reader["nome"].ToString();
					person.Category = Convert.ToInt32(reader["tipo"]);
					person.PersonType = Convert.ToInt32(reader["natureza"]);
					person.Document = reader["documento"].ToString();
				}
				reader.Close();
				//this.databaseHelper.CloseConection();
			}
			finally
			{
				if (reader != null)
				{
					reader.Close();
				}
				//this.databaseHelper.CloseConection();
			}
			return (T)person;
		}
		
		public bool DeleteRegistry(T registry)
		{
			throw new NotImplementedException();
		}
		
		public DataTable GetAllDataTable()
		{
			return (DataTable)ConnectionDB.executeCommand("select * from person",CommandType.Text, null, TypeCommand.ExecuteDataTable);
		}
		
		public bool DeleteRegistryById(int id)
		{
			throw new NotImplementedException();
		}
	}
}
